package com.tec.security.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tec.security.entity.News;
import com.tec.security.entity.ViewsIP;
import com.tec.security.mapper.NewsMapper;
import com.tec.security.mapper.ViewsIPMapper;
import com.tec.security.service.ViewsIPService;
import com.tec.security.utils.date.DateString;
import com.tec.security.utils.response.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tec.security.entity.Views;
import com.tec.security.mapper.ViewsMapper;
import com.tec.security.service.ViewsService;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author LuLin
 * @description 针对表【homeviews】的数据库操作Service实现
 * @createDate 2023-10-21 09:22:50
 */
@Service
@Transactional
public class ViewsServiceImpl extends ServiceImpl<ViewsMapper,Views> implements ViewsService {

    private final ViewsMapper homeviewsMapper;
    private final NewsMapper newsMapper;
    private final ViewsIPMapper viewsIPMapper;
    private final ViewsIPService viewsIPService;
    @Autowired
    public ViewsServiceImpl(ViewsMapper homeviewsMapper, NewsMapper newsMapper, ViewsIPMapper viewsIPMapper, ViewsIPService viewsIPService) {
        this.homeviewsMapper = homeviewsMapper;
        this.newsMapper = newsMapper;
        this.viewsIPMapper = viewsIPMapper;
        this.viewsIPService = viewsIPService;
    }

    public int allHomeViews() {
        int AllPageViewsNumber = 0;
        QueryWrapper<News> newsQueryWrapper = new QueryWrapper<>();
        newsQueryWrapper.select("views");
        List<News> PageViewsList = newsMapper.selectList(newsQueryWrapper);
        for (News news : PageViewsList) {
            AllPageViewsNumber += news.getViews();
        }
        QueryWrapper<Views> regularVisitorsQueryWrapper = new QueryWrapper<>();
        regularVisitorsQueryWrapper.select("views");
        List<Views> regularVisitorsList = homeviewsMapper.selectList(regularVisitorsQueryWrapper);
        for (Views regularVisitors : regularVisitorsList) {
            AllPageViewsNumber += regularVisitors.getViews();
        }
        return AllPageViewsNumber;
    }

    public int todayHomeViews() {
        QueryWrapper<Views> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("date", DateString.currentDate());
        Views views = homeviewsMapper.selectOne(queryWrapper);
        if (views != null) {
            int result = views.getViews() + 1;
            UpdateWrapper<Views> homeviewsUpdateWrapper = new UpdateWrapper<>();
            homeviewsUpdateWrapper.eq("date", DateString.currentDate()).set("views", result);
            homeviewsMapper.update(null, homeviewsUpdateWrapper);
            return result;
        } else {
            Views _views = new Views();
            _views.setViews(1);
            _views.setDate(DateString.currentDate());
            homeviewsMapper.insert(_views);
            return 1;
        }
    }

    @Override
    public Result allData() {
        Map<String,Object> result=new HashMap<>();
        QueryWrapper<Views> viewsQueryWrapper=new QueryWrapper<>();
        viewsQueryWrapper.eq("date",DateString.currentDate());
        QueryWrapper<ViewsIP> viewsIPQueryWrapper=new QueryWrapper<>();
        viewsIPQueryWrapper.eq("date",DateString.currentDate());
        QueryWrapper<News> newsQueryWrapper=new QueryWrapper<>();
        newsQueryWrapper.eq("status",5);
        int newsViews=0;
        int views=0;
        List<News> newsList=newsMapper.selectList(newsQueryWrapper);
        for (News i:newsList) {
            newsViews+=i.getViews();
        }
        QueryWrapper<Views> viewsQueryWrapper1=new QueryWrapper<>();
        List<Views> viewsList=homeviewsMapper.selectList(viewsQueryWrapper1);
        for (Views j:viewsList) {
            views+=j.getViews();
        }
        result.put("allNewsViews",newsViews);
        result.put("newsNumber",newsList.size());
        result.put("todayViews",homeviewsMapper.selectOne(viewsQueryWrapper).getViews());
        result.put("todayIPViews",viewsIPMapper.selectCount(viewsIPQueryWrapper));
        result.put("allIPViews",viewsIPService.allIP());
        result.put("allViews",views);
        return Result.success("请求成功",result);
    }
}




